﻿using Fiddler;
using Newtonsoft.Json;
using ProgramCat.FiddlerExtension;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace ProgramCat.EPlug
{
    public class PlugCoreMgr : Singleton<PlugCoreMgr>
    {
        private PlugCoreMgr() { }
        dynamic AppCom = null;
        /// <summary>
        /// 这是一个插件的集合
        /// </summary>
        List<EPlugInfo> ePlugs = new List<EPlugInfo>();
        /// <summary>
        /// 初始化插件
        /// </summary>
        public override void OnSingletonInit()
        {
            try
            {
                //动态调用com组件
                System.Type t = Type.GetTypeFromProgID("ProgramCat.AppCom");
                //反射拿到类
                AppCom = Activator.CreateInstance(t);
                //反射调用方法
                string str_Plugs = AppCom.LoadAllPlugs(Directory.GetCurrentDirectory());
                //类到json
                ePlugs = JsonConvert.DeserializeObject<List<EPlugInfo>>(str_Plugs);
            }
            catch 
            {
            }
            
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public List<EPlugInfo> Init()
        {
            return ePlugs;
        }

        /// <summary>
        /// 设置插件开启状态
        /// </summary>
        /// <param name="Tag"></param>
        /// <param name="checked_"></param>
        public void SetPlugOpen(int Tag, bool checked_)
        {
            ePlugs.ForEach((x) =>
            {
                if (x.plugPtr == Tag)
                {
                    x.isopen = checked_;
                }  
            });
        }

        /// <summary>
        /// 当请求发起事件
        /// </summary>
        /// <param name="oSession"></param>
        public void OnRequest(Session oSession)
        {
            foreach (EPlugInfo item in ePlugs)
            {
                if (item.isopen && AppCom!=null)
                {
                    MemoryStream stream = new MemoryStream();
                    
                    oSession.WriteRequestToStream(false, true, stream);
                    byte[] requestBytes = stream.ToArray();
                    stream.Dispose();
                    stream.Close();
                    byte[] newRequestBytes = AppCom.OnRequest(item.plugPtr, requestBytes);
                    //if (newRequestBytes.Length == 0)
                    //{
                        
                    //}
                }
            }
            // //MemoryStream stream = new MemoryStream();
            // session.WriteRequestToStream(true, true,true, stream);
            //File.WriteAllBytes(@"C:\Users\unity\Desktop\project\OnRequest.txt", stream.ToArray());

            //new Session();
           
            //HTTPRequestHeaders hTTPHeaderItems = Fiddler.Parser.ParseRequest();
            //hTTPHeaderItems["11"]
            //Fiddler.Parser.

            //Fiddler.Utilities.ReadSessionArchive()

            //Fiddler.Utilities.WriteSessionArchive

        }



    }
    public class EPlugInfo
    {
        /// <summary>
        /// 插件指针
        /// </summary>
        public int plugPtr = 0;
        /// <summary>
        /// 文件名称
        /// </summary>
        public string fileName = "";
        /// <summary>
        /// 插件名称
        /// </summary>
        public string plugName = "";
        /// <summary>
        /// 作者名称
        /// </summary>
        public string plugAuthor = "";
        /// <summary>
        /// 版本号
        /// </summary>
        public string version = "";
        /// <summary>
        /// 调用成功次数
        /// </summary>
        public int successNumber = 0;
        /// <summary>
        /// 是否开启
        /// </summary>
        public bool isopen = false;
    }
}
